/*
 * mainwindow.cpp
 *
 *  Created on: 08.09.2008
 *      Author: Администратор
 */

#include <QtGui>

#include "mainwindow.h"

MainWindow::MainWindow()
{
	createActions();

	statusButton = new QToolButton();
	statusButton->setAutoRaise(true);
	statusButton->setArrowType(Qt::NoArrow);
	statusButton->setIcon(QIcon("images/statusoffline.png"));

	mainMenu = new QToolButton();
	mainMenu->setAutoRaise(true);
	mainMenu->setArrowType(Qt::NoArrow);

	progress = new QProgressBar;
	progress->setRange(0, 100);
	progress->setValue(34);
	progress->setAlignment(Qt::AlignHCenter);
	progress->setVisible(false);

	createContextMenu();

	contactList = new QListWidget;
	contactList->addItem(tr("Федот"));

	mainLayout = new QVBoxLayout;
	nizMenu = new QHBoxLayout;

	mainLayout->addWidget(contactList);
	mainLayout->addLayout(nizMenu);
	mainLayout->setMargin(0);

	nizMenu->addWidget(mainMenu);
	nizMenu->addWidget(statusButton);
	nizMenu->addStretch(0);
	nizMenu->addWidget(progress);

	statusButton->setPopupMode(QToolButton::InstantPopup);
	mainMenu->setPopupMode(QToolButton::InstantPopup);

	mainMenu->setMenu(mainContextMenu);
	statusButton->setMenu(statusContextMenu);

//	setWindowFlags(Qt::Tool);
	setLayout(mainLayout);

	readSetting();
}

void MainWindow::createActions()
{
	onlineStatus = new QAction(tr("Онлайн"), this);
	onlineStatus->setIcon(QIcon("images/statusonline.png"));
	connect(onlineStatus, SIGNAL(triggered()), this, SLOT(changeStatusToOnline()));

	offlineStatus = new QAction(tr("Оффлайн"), this);
	offlineStatus->setIcon(QIcon("images/statusoffline.png"));
	connect(offlineStatus, SIGNAL(triggered()), this, SLOT(changeStatusToOffline()));

	exitAction = new QAction(tr("Выход"), this);
	exitAction->setIcon(QIcon("images/exitbutton.png"));
	connect(exitAction, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));

	aboutAction = new QAction(tr("О программе"), this);
	aboutAction->setIcon(QIcon("images/aboutbutton.png"));
}

void MainWindow::createContextMenu()
{
	statusContextMenu = new QMenu;
	statusContextMenu->addAction(onlineStatus);
	statusContextMenu->addSeparator();
	statusContextMenu->addAction(offlineStatus);

	mainContextMenu = new QMenu;
	mainContextMenu->addAction(aboutAction);
	mainContextMenu->addSeparator();
	mainContextMenu->addAction(exitAction);
}

/////////////////////////////////////////////////////////
//Начало реализации слотов и функций
/////////////////////////////////////////////////////////

void MainWindow::changeStatusToOnline()
{
	statusButton->setIcon(QIcon("images/statusonline.png"));
}

void MainWindow::changeStatusToOffline()
{
	statusButton->setIcon(QIcon("images/statusoffline.png"));
}

void MainWindow::closeEvent(QCloseEvent *event)
{
	writeSetting();
}

void MainWindow::writeSetting()
{
	QSettings settings("Fedot", "FedotIM");

	settings.setValue("geometry", geometry());
}

void MainWindow::readSetting()
{
	QSettings settings("Fedot", "FedotIM");
	QRect rect = settings.value("geometry", QRect(150, 150, 150, 300)).toRect();

	move(rect.topLeft());
	resize(rect.size());
}
